-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPower.vb
182 lines (169 loc) · 7.12 KB
/
Power.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
Namespace My
''' <summary>
''' 电源管理计划相关函数
''' </summary>
''' <remarks></remarks>
Partial Public NotInheritable Class Power
''' <summary>
''' 设定关机计划(同步阻塞,注意会覆盖之前设定的关机/重启计划)
''' </summary>
''' <param name="DelaySecond">延时时间(单位秒,最大值315360000,10年,默认值为0,立即执行)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function ShutDown(Optional ByVal DelaySecond As Integer = 0) As Boolean
Try
Dim p As New Process
p.StartInfo.FileName = "cmd.exe"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.CreateNoWindow = True
p.Start()
p.StandardInput.WriteLine("shutdown -a >nul 2>nul")
p.StandardInput.WriteLine("shutdown -s -t " & DelaySecond & " >nul 2>nul")
p.StandardInput.WriteLine("exit")
p.StandardOutput.ReadToEnd()
p.StandardOutput.Close()
p.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 设定关机计划(异步执行,注意会覆盖之前设定的关机/重启计划)
''' </summary>
''' <param name="DelaySecond">延时时间(单位秒,最大值315360000,10年,默认值为0,立即执行)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function ShutDownAsync(Optional ByVal DelaySecond As Integer = 0) As Boolean
Try
Shell("shutdown -a", AppWinStyle.Hide, False)
Shell("shutdown -s -t " & DelaySecond, AppWinStyle.Hide, False)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 设定重启计划(同步阻塞,注意会覆盖之前设定的关机/重启计划)
''' </summary>
''' <param name="DelaySecond">延时时间(单位秒,最大值315360000,10年,默认值为0,立即执行)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function Reboot(Optional ByVal DelaySecond As Integer = 0) As Boolean
Try
Dim p As New Process
p.StartInfo.FileName = "cmd.exe"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.CreateNoWindow = True
p.Start()
p.StandardInput.WriteLine("shutdown -a >nul 2>nul")
p.StandardInput.WriteLine("shutdown -r -t " & DelaySecond & " >nul 2>nul")
p.StandardInput.WriteLine("exit")
p.StandardOutput.ReadToEnd()
p.StandardOutput.Close()
p.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 设定重启计划(异步执行,注意会覆盖之前设定的关机/重启计划)
''' </summary>
''' <param name="DelaySecond">延时时间(单位秒,最大值315360000,10年,默认值为0,立即执行)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function RebootAsync(Optional ByVal DelaySecond As Integer = 0) As Boolean
Try
Shell("shutdown -a", AppWinStyle.Hide, False)
Shell("shutdown -r -t " & DelaySecond, AppWinStyle.Hide, False)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 取消所有计划(同步阻塞,之前没有关机/重启计划时则无效果)
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function AbortPlan() As Boolean
Try
Dim p As New Process
p.StartInfo.FileName = "cmd.exe"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.CreateNoWindow = True
p.Start()
p.StandardInput.WriteLine("shutdown -a >nul 2>nul")
p.StandardInput.WriteLine("exit")
p.StandardOutput.ReadToEnd()
p.StandardOutput.Close()
p.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 取消所有计划(异步执行,之前没有关机/重启计划时则无效果)
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function AbortPlanAsync() As Boolean
Try
Shell("shutdown -a", AppWinStyle.Hide, False)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 锁定电脑(同步阻塞,立即执行)
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function Lock() As Boolean
Try
Shell("rundll32.exe user32.dll LockWorkStation", AppWinStyle.Hide, True)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 注销用户(同步阻塞,立即执行)
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function Logout() As Boolean
Try
Shell("shutdown -l", AppWinStyle.Hide, True)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 休眠电脑(同步阻塞,立即执行,注意系统必须启用了休眠功能才会有效果)
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function Hibernate() As Boolean
Try
Shell("shutdown -h", AppWinStyle.Hide, True)
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
End Namespace